package cn.m3a1.creditsystem.controller;

import cn.m3a1.creditsystem.bean.SchoolSchedule;
import cn.m3a1.creditsystem.bean.StudentSchedule;
import cn.m3a1.creditsystem.bean.User;
import cn.m3a1.creditsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wyj
 * @date 2019/12/29
 * @time 下午5:43
 */
@Controller
public class UpdateController {
    @Autowired
    SchoolScheduleService scheduleService;
    @Autowired
    StudentScheduleService studentScheduleService;
    @Autowired
    TrainingPlanService trainingPlanService;
    @Autowired
    StudentResultService studentResultService;
    @RequestMapping("/hi")
    public String sayHello() {
        System.out.println("运行了。。。");
        return "redirect:/index.html";
    }


    @Autowired
    private ImportService importService;


    @PostMapping(value = "/upload")
    @ResponseBody
    public String uploadExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");
        if (file.isEmpty()) {
            return "文件不能为空";
        }
        InputStream inputStream = file.getInputStream();
        List<List<String>> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename());
        inputStream.close();

        for (int i = 0; i < list.size(); i++) {
            List<String> lo = list.get(i);
            //TODO 随意发挥
            System.out.println(lo);

        }
        return "上传成功";
    }


    @PostMapping(value = "/uploads")
    public String uploadExcels(HttpServletRequest request) throws Exception {

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return null;
        }

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> fileList = multipartRequest.getFiles("file");
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Collection<MultipartFile> files = fileMap.values();
        System.out.println(fileList.size());
        for(MultipartFile file:fileList){
            InputStream inputStream = file.getInputStream();
            List<List<String>> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();
            System.out.println(list.get(1).get(0));
            if ("课程代码".equals(list.get(0).get(0).toString())){
                System.out.println("true");
                Boolean b= scheduleService.inserts(list);
                if (b){
                    List<SchoolSchedule> schoolSchedules = scheduleService.selectAll();
                    Boolean aBoolean = scheduleService.inserts_model(schoolSchedules);
                }

            }else if ("选课课号".equals(list.get(1).get(0).toString())){
                Boolean b = studentScheduleService.inserts(list);
                if (b){
                    List<StudentSchedule> studentSchedules = studentScheduleService.selectAll();
                    Boolean aBoolean = studentScheduleService.inserts_model(studentSchedules);
                }
                //System.out.println("true");

            }else if ("课程性质".equals(list.get(0).get(0).toString())){
                    trainingPlanService.inserts(list);

            }else if ("学号".equals(list.get(1).get(0).toString())){
                studentResultService.inserts(list);

            }



//            for (int i = 0; i < list.size(); i++) {
//                List<String> lo = list.get(i);
//                //TODO 随意发挥
//
//                System.out.println(lo);
//
//
//            }
            System.out.println("---------------------------分割线----------------------------");
        }
        if (fileMap.isEmpty()) {
            return "文件不能为空";
        }


        return "redirect:/index.html";
    }

}
